﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SmallERP.MODEL.FoundationModel;
using DevExpress.XtraEditors;
using SmallERP.Bll;
using SmallERP.Bll.BaseInfo;
namespace SmallERP.Hosting.Frm
{
    public partial class FrmBaseInfo : Form
    {
        #region 私有变量

        private Printer_info    _printerInfo = null;
        private PaperFace_Info  _paperFaceInfo = null;

        private PaperType_Info  _paperTypeInfo = null;
        private SysSetting_info _sysSetInfo = null;

        private GridSubArea_Info _subAreaInfo = null;

        private IDBAccess       iDBAccess =null;
      
        #endregion
        public FrmBaseInfo()
        {
            InitializeComponent();
        }

        private void xtraTabControl_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            switch (e.Page.Name)
            {
                case "xtPagePrinter":
                    iDBAccess = new Printer();
                    iDBAccess.LinkGridControl(gcPrinter);
                    break;
                case "xtPagePaperFace":
                    iDBAccess = new Paperface();
                    iDBAccess.LinkGridControl(gcPaperFace);
                    break;
                case "xtPagePaperType":
                    iDBAccess = new Papertype();
                    iDBAccess.LinkGridControl(gcPaperType);
                    break;
                case "xtPageFilePath":
                    GetFilePath();
                    break;
                case "xtPageSubArea":
                    iDBAccess = new Subarea();
                    iDBAccess.LinkGridControl(gcSubArea);
                    break;

            }
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //绑定打印机
            iDBAccess = new Printer();
            iDBAccess.LinkGridControl(gcPrinter);

            //绑定纸型
            LinkPaperType();
        }


        #region 打印机


        //添加打印机
        private void btnAddPrinter_Click(object sender, EventArgs e)
        {
            iDBAccess = new Printer();
            Cursor = Cursors.WaitCursor;
            btnAddPrinter.Enabled = false;
            if(DataCheck())
            {
                Printer_info printerInfo = new Printer_info();
                printerInfo.HotDirPath =txtHotFilePath.Text;
                printerInfo.IsTransverse =chkIsHorizontalPrint.Checked;
                printerInfo.MonitorDirPath =txtMonitorFilePath.Text;
                printerInfo.PBstatus =0;
                printerInfo.PDes =txtPrinterDes.Text;
                printerInfo.PDPI =Convert.ToInt32( txtDPI.Text);
                printerInfo.PLength =Convert.ToDouble( txtPrinterLength.Text);
                printerInfo.PName =txtPrinterName.Text;
                printerInfo.PrintStatus =0;
                printerInfo.PWidth =Convert.ToDouble(txtPrinterWidth.Text);

                int ret = iDBAccess.Add(printerInfo);
                if (ret ==-1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcPrinter);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该打印机在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnAddPrinter.Enabled = true;
        }
        //更新打印机
        private void btnUpdatePrinter_Click(object sender, EventArgs e)
        {
            iDBAccess = new Printer();
            Cursor = Cursors.WaitCursor;
            btnUpdatePrinter.Enabled = false;
            if (DataCheck())
            {
                Printer_info printerInfo = new Printer_info();
                printerInfo.PrinterID = _printerInfo.PrinterID;
                printerInfo.HotDirPath = txtHotFilePath.Text;
                printerInfo.IsTransverse = chkIsHorizontalPrint.Checked;
                printerInfo.MonitorDirPath = txtMonitorFilePath.Text;
                printerInfo.PBstatus = 0;
                printerInfo.PDes = txtPrinterDes.Text;
                printerInfo.PDPI = Convert.ToInt32(txtDPI.Text);
                printerInfo.PLength = Convert.ToDouble(txtPrinterLength.Text);
                printerInfo.PName = txtPrinterName.Text;
                printerInfo.PrintStatus = 0;
                printerInfo.PWidth = Convert.ToDouble(txtPrinterWidth.Text);

                int ret = iDBAccess.Update(printerInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcPrinter);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("数据库中已经存在一个该名称的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnUpdatePrinter.Enabled = true;

        }

        //删除打印机
        private void btnDeletePrinter_Click(object sender, EventArgs e)
        {
            iDBAccess = new Printer();
            Cursor = Cursors.WaitCursor;
            btnDeletePrinter.Enabled = false;
            if (_printerInfo != null)
            {
                int ret = iDBAccess.Delete(_printerInfo);
                if (ret == -1)
                {
                    MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    iDBAccess.LinkGridControl(gcPrinter);
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的打印机", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Cursor = Cursors.Default;
            btnDeletePrinter.Enabled = true;
        }
        private bool DataCheck()
        {


            if (IsTextNull(txtPrinterName, "打印机名称"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterDes, "打印机描述"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterLength, "打印纸长"))
            {
                return false;
            }
            if (IsTextNull(txtPrinterWidth, "打印纸宽"))
            {
                return false;
            }
            if (IsTextNull(txtHotFilePath, "热文件夹路径"))
            {
                return false;
            }
            if (IsTextNull(txtMonitorFilePath, "监控文件夹路径"))
            {
                return false;
            }
            if (IsTextNull(txtDPI, "打印机DPI"))
            {
                return false;
            }
            return true;

        }
        /// <summary>
        /// 判断文本框是否为空
        /// </summary>
        /// <param name="text"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        private bool IsTextNull(TextEdit text, string name)
        {
            if (string.IsNullOrEmpty(text.Text.Trim()))
            {
                MessageBox.Show("请输入"+name, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                text.Focus();
                return true;
            }
            return false;
        }
        //焦点行改变事件
        private void gvPrinter_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _printerInfo = (Printer_info)gvPrinter.GetFocusedRow();
            if (_printerInfo != null)
            {
                txtDPI.Text = _printerInfo.PDPI.ToString();
                txtHotFilePath.Text = _printerInfo.HotDirPath;
                txtMonitorFilePath.Text = _printerInfo.MonitorDirPath;
                txtPrinterDes.Text = _printerInfo.PDes;
                txtPrinterLength.Text = _printerInfo.PLength.ToString();
                txtPrinterName.Text = _printerInfo.PName;
                txtPrinterWidth.Text = _printerInfo.PWidth.ToString();
                chkIsHorizontalPrint.Checked = _printerInfo.IsTransverse;
            }
        }
        //选择监控文件夹
        private void btnMonitorFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtMonitorFilePath.Text = path;

            }

        }
        //选择热文件夹
        private void btnHotFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtHotFilePath.Text = path;
            }
        }

        //选择ICC文件
        private void btnICCFileBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog.ShowDialog())
            {
                txtICCFilePath.Text = openFileDialog.FileName;
            }
        }
        //设置打印机ICC路径及锐化值
        private void btnICCOK_Click(object sender, EventArgs e)
        {

            PrinterSet printerSet = new PrinterSet();
            Papertype papertype = new Papertype();
            if(string.IsNullOrEmpty(cmbPrinterPaperType.Text.Trim()))
            {
                MessageBox.Show("请选择纸型", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if(string.IsNullOrEmpty(txtSharpValue.Text.Trim()))
            {
                MessageBox.Show("请输入锐化值", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtSharpValue.Focus();
                return;
            }
            PrinterSeting_info printerSetInfo =printerSet.GetPrinterIccPath(_printerInfo.PrinterID,cmbPrinterPaperType.Text);
            if (printerSetInfo != null)
            {
                printerSetInfo.ICCPATH = txtICCFilePath.Text;
                printerSetInfo.Sharpening = Convert.ToInt32(txtSharpValue.Text);

                int ret = printerSet.Update(printerSetInfo);
                if (ret == 1)
                {
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("设置失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                printerSetInfo = new PrinterSeting_info();
                printerSetInfo.PrinterId = _printerInfo.PrinterID;
                printerSetInfo.PaperTypeId = papertype.GetPaperType(cmbPrinterPaperType.Text).PaperTypeId;
                printerSetInfo.ICCPATH = txtICCFilePath.Text;
                printerSetInfo.Sharpening =Convert.ToInt32(txtSharpValue.Text);
                int ret = printerSet.Insert(printerSetInfo);
                if (ret == 1)
                {
                    MessageBox.Show("设置成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("设置失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        /// <summary>
        /// 绑定纸型
        /// </summary>
        private void LinkPaperType()
        {
            Papertype paperType = new Papertype();
            cmbPrinterPaperType.Properties.Items.Clear();
            foreach (var item in paperType.GetPaperTypeList())
            {
                cmbPrinterPaperType.Properties.Items.Add(item.PaperTypeName);
            }
        }

        private void cmbPrinterPaperType_SelectedIndexChanged(object sender, EventArgs e)
        {
            PrinterSet printerSet = new PrinterSet();
            PrinterSeting_info printerSetInfo = printerSet.GetPrinterIccPath(_printerInfo.PrinterID, cmbPrinterPaperType.Text);
            if (printerSetInfo != null)
            {
                txtSharpValue.Text = printerSetInfo.Sharpening.ToString();
                txtICCFilePath.Text = printerSetInfo.ICCPATH;
            }
            else
            {
                 txtSharpValue.Text = string.Empty;
                 txtICCFilePath.Text = string.Empty;           
            }
        }
        #endregion

        #region 纸面

        private void gvPaperFace_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperFaceInfo = (PaperFace_Info)gvPaperFace.GetFocusedRow();
            if (_paperFaceInfo != null)
            {
                txtPaperFace.Text = _paperFaceInfo.PaperFaceName;
            }

        }

        //添加纸面
        private void btnAddPaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnAddPaperFace.Enabled = false;
            if (!IsPaperFaceTextNull())
            {

                PaperFace_Info paperFaceInfo = new PaperFace_Info();
                paperFaceInfo.PaperFaceName = txtPaperFace.Text;
                int ret = iDBAccess.Add(paperFaceInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret >0)
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该纸面在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnAddPaperFace.Enabled = true;
        }

        private bool IsPaperFaceTextNull()
        {
            if (string.IsNullOrEmpty(txtPaperFace.Text.Trim()))
            {
                MessageBox.Show("请输入纸面名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPaperFace.Focus();
                return true;
            }

            return false;
        }
        //更新纸面
        private void btnUpdatePaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnUpdatePaperFace.Enabled = false;
            if (!IsPaperFaceTextNull())
            {

                PaperFace_Info paperFaceInfo = new PaperFace_Info();
                paperFaceInfo.PaperFaceName = txtPaperFace.Text;
                paperFaceInfo.PaperFaceId = _paperFaceInfo.PaperFaceId;
                int ret = iDBAccess.Update(paperFaceInfo);
                if (ret == -2)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret > 0)
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnUpdatePaperFace.Enabled = true;
            
        }
        //删除纸面
        private void btnDeletePaperFace_Click(object sender, EventArgs e)
        {
            iDBAccess = new Paperface();
            Cursor = Cursors.WaitCursor;
            btnDeletePaperFace.Enabled = false;
            if (_paperFaceInfo != null)
            {
                int ret = iDBAccess.Delete(_paperFaceInfo);
                if (ret == -1)
                {
                    MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    iDBAccess.LinkGridControl(gcPaperFace);
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请选择要删除的纸面", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

            Cursor = Cursors.Default;
            btnDeletePaperFace.Enabled = true;

        }
        
        #endregion

        #region 纸型

        private void gvPaperType_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _paperTypeInfo =(PaperType_Info)gvPaperType.GetFocusedRow();
            if (_paperTypeInfo != null)
            {
                txtPaperType.Text = _paperTypeInfo.PaperTypeName;
            }
        }

        //添加纸型
        private void btnPaperTypeAdd_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeAdd.Enabled = false;
            if (!IsPaperTypeTextNull())
            {

                PaperType_Info paperTypeInfo = new PaperType_Info();
                paperTypeInfo.PaperTypeName = txtPaperType.Text;
                int ret = iDBAccess.Add(paperTypeInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPaperType);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该纸型在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeAdd.Enabled = true;

        }
        private bool IsPaperTypeTextNull()
        {
            if (string.IsNullOrEmpty(txtPaperType.Text.Trim()))
            {
                MessageBox.Show("请输入纸型名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtPaperType.Focus();
                return true;
            }

            return false;
 
        }
        //更新纸型
        private void btnPaperTypeUpdate_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeUpdate.Enabled = false;
            if (!IsPaperTypeTextNull())
            {

                PaperType_Info paperTypeInfo = new PaperType_Info();
                paperTypeInfo.PaperTypeId = _paperTypeInfo.PaperTypeId;
                paperTypeInfo.PaperTypeName = txtPaperType.Text;
                int ret = iDBAccess.Update(paperTypeInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 1)
                {
                    iDBAccess.LinkGridControl(gcPaperType);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("纸型未找到", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeUpdate.Enabled = true;

        }
        //删除纸型
        private void btnPaperTypeDelete_Click(object sender, EventArgs e)
        {
            iDBAccess = new Papertype();
            Cursor = Cursors.WaitCursor;
            btnPaperTypeDelete.Enabled = false;
            if (!IsPaperTypeTextNull())
            {
                if (_paperTypeInfo != null)
                {

                    int ret = iDBAccess.Delete(_paperTypeInfo);
                    if (ret == -1)
                    {
                        MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        iDBAccess.LinkGridControl(gcPaperType);
                        MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            Cursor = Cursors.Default;
            btnPaperTypeDelete.Enabled = true;

        }

        #endregion

        #region 文件路径

        private void GetFilePath()
        {
            SysSet sysSet = new SysSet();
            _sysSetInfo = sysSet.GetSysSet();
            txtBigPicPath.Text = _sysSetInfo.BigPicSavePath;
            txtOrderPath.Text = _sysSetInfo.SysOrderPath;
            
        }

        private void btnBigPicPathBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtBigPicPath.Text = path;
            }
        }

        private void btnOrderPathBrowser_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == folderBrowserDialog.ShowDialog())
            {
                string path = folderBrowserDialog.SelectedPath;
                if (!path.Contains(@"\\"))
                {
                    MessageBox.Show("请选择网络路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                txtOrderPath.Text = path;
            }

        }

        private void btnFilePathUpdate_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtBigPicPath.Text.Trim()))
            {
                MessageBox.Show("请选择生成大图文件夹路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            if (string.IsNullOrEmpty(txtOrderPath.Text.Trim()))
            {
                MessageBox.Show("请选择订单文件夹路径", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            SysSet sysSet = new SysSet();

            SysSetting_info sysSetInfo = new SysSetting_info();
            sysSetInfo.BigPicSavePath = txtBigPicPath.Text;
            sysSetInfo.SysOrderPath = txtOrderPath.Text;
            int ret = sysSet.Update(sysSetInfo);
            if (ret == 0)
            {
                MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        #endregion

   

        #region 格子分区

        private void gvSubArea_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            _subAreaInfo = (GridSubArea_Info)gvSubArea.GetFocusedRow();
            if (_subAreaInfo != null)
            {
                txtHeardTitle.Text = _subAreaInfo.NameHeadChar;
                txtHeightNum.Text = _subAreaInfo.HeightNum.ToString();
                txtOutTimeAlarm.Text = _subAreaInfo.OutTimeAlarmTime.ToString();
                txtSubAreaName.Text = _subAreaInfo.SubAreaName;
                txtWidthNum.Text = _subAreaInfo.WidthNum.ToString();
            }
        }
        //添加分区
        private void btnSubAreaAdd_Click(object sender, EventArgs e)
        {
            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaAdd.Enabled = false;
            if (SubAreaDataCheck())
            {

                GridSubArea_Info subAreaInfo = new GridSubArea_Info();
                subAreaInfo.HeightNum = Convert.ToInt32(txtHeightNum.Text);
                subAreaInfo.IsUseful = chkSubAreaIsUse.Checked;
                subAreaInfo.NameHeadChar = txtHeardTitle.Text;
                subAreaInfo.OutTimeAlarmTime = Convert.ToInt32(txtOutTimeAlarm.Text);
                subAreaInfo.SubAreaName = txtSubAreaName.Text;
                subAreaInfo.WidthNum = Convert.ToInt32(txtWidthNum.Text);

                int ret = iDBAccess.Add(subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("添加失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该分区在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaAdd.Enabled = true;

        }

        //更新分区
        private void btnSubAreaUpdate_Click(object sender, EventArgs e)
        {
            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaUpdate.Enabled = false;
            if (SubAreaDataCheck())
            {

                GridSubArea_Info subAreaInfo = new GridSubArea_Info();
                subAreaInfo.SubAreaID = _subAreaInfo.SubAreaID;
                subAreaInfo.HeightNum = Convert.ToInt32(txtHeightNum.Text);
                subAreaInfo.IsUseful = chkSubAreaIsUse.Checked;
                subAreaInfo.NameHeadChar = txtHeardTitle.Text;
                subAreaInfo.OutTimeAlarmTime = Convert.ToInt32(txtOutTimeAlarm.Text);
                subAreaInfo.SubAreaName = txtSubAreaName.Text;
                subAreaInfo.WidthNum = Convert.ToInt32(txtWidthNum.Text);

                int ret = iDBAccess.Update(subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("更新失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("更新成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("该分区在数据库中已经存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaUpdate.Enabled = true;
        }
        //删除分区
        private void btnSubAreaDelete_Click(object sender, EventArgs e)
        {

            iDBAccess = new Subarea();
            Cursor = Cursors.WaitCursor;
            btnSubAreaDelete.Enabled = false;
            if (SubAreaDataCheck())
            {


                int ret = iDBAccess.Delete(_subAreaInfo);
                if (ret == -1)
                {
                    MessageBox.Show("删除失败，请检查数据库连接", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else if (ret == 0)
                {
                    iDBAccess.LinkGridControl(gcSubArea);
                    MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            Cursor = Cursors.Default;
            btnSubAreaDelete.Enabled = true;
        }
        /// <summary>
        /// 分区数据检查
        /// </summary>
        /// <returns></returns>
        private bool SubAreaDataCheck()
        {
            if (IsTextNull(txtSubAreaName, "分区名称"))
            {
                return false;
            }
            if (IsTextNull(txtWidthNum, "横向格子数"))
            {
                return false;
            }
            if (IsTextNull(txtSubAreaName, "分区名称"))
            {
                return false;
            }
            if (IsTextNull(txtHeightNum, "纵向格子数"))
            {
                return false;
            }
            if (IsTextNull(txtHeardTitle, "头字母"))
            {
                return false;
            }
            if (IsTextNull(txtOutTimeAlarm, "超时报警时间"))
            {
                return false;
            }

            return true;
        }
        #endregion

        private void btncali_Click(object sender, EventArgs e)
        {
            //ClassVectorPointClass vec = new ClassVectorPointClass();
            //string a = vec.CalibrationWirzard();
        }

   

    }
}
